package pl.vanta.mjuzik2.impl.playlists.types;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import pl.vanta.mjuzik2.api.AudioFile;
import pl.vanta.mjuzik2.impl.playlists.AbstractPlaylistType;

public class PlsPlaylistType extends AbstractPlaylistType
{
	private static final String	IDENTIFIER	= "[playlist]";

	@Override
	public String getDescription()
	{
		return "Pls files";
	}

	@Override
	public String[] getExtensions()
	{
		return new String[] { "pls" };
	}

	private boolean acceptPlaylist(String firstLine)
	{
		return IDENTIFIER.equals(firstLine);
	}

	@Override
	public List<File> loadPlaylist(File playlist)
	{
		List<File> files = new ArrayList<File>();

		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(playlist), Charset.defaultCharset()));
			String line = br.readLine();

			if (acceptPlaylist(line))
			{
				while ((line = br.readLine()) != null)
				{
					if (line.startsWith("File"))
					{
						int index = line.indexOf('=');

						if (index != -1)
						{
							String filename = line.substring(index + 1);

							File file = new File(filename);

							if (file.exists() && file.isFile())
							{
								// normalny plik
								files.add(file);
							}
							else
							{
								// najprawdopodobniej wzgledna sciezka
								file = new File(playlist.getParent() + File.separatorChar + filename);

								if (file.exists() && file.isFile())
								{
									files.add(file);
								}
							}
						}
					}
				}

				return files;
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		return null;
	}
	
	@Override
	public String generatePlaylist(File file, List<AudioFile> audioFiles, boolean relative)
	{
		String filePrefix = "File";
		String titlePrefix = "Title";
		String lengthprefix = "Length";
		
		StringBuilder sb = new StringBuilder();
		sb.append(IDENTIFIER);
		sb.append(NEW_LINE);
		sb.append("NumberOfEntries=");
		sb.append(audioFiles.size());
		sb.append(NEW_LINE);
		sb.append(NEW_LINE);
		
		String directory = file.getParent();
		if(directory.charAt(directory.length() - 1) != File.separatorChar)
		{
			directory += File.separatorChar;
		}
		
		int counter = 0;
		
		for (AudioFile audioFile : audioFiles)
		{
			counter++;

			String path = audioFile.getFile().getParent() + File.separatorChar;

			if(relative)
			{
				int index = audioFile.getFile().getParent().indexOf(directory);
				
				if(index != -1)
				{
					path = audioFile.getFile().getParent().substring(index + directory.length()) + File.separatorChar;
				}
			}
			
			sb.append(filePrefix);
			sb.append(counter);
			sb.append('=');
			sb.append(path + audioFile.getFile().getName());
			sb.append(NEW_LINE);

			sb.append(titlePrefix);
			sb.append(counter);
			sb.append('=');
			sb.append(audioFile.getFileInfo().getArtist());
			sb.append(" - ");
			sb.append(audioFile.getFileInfo().getTitle());
			sb.append(NEW_LINE);
			
			sb.append(lengthprefix);
			sb.append(counter);
			sb.append('=');
			sb.append(audioFile.getFileInfo().getPlayTime() / 1000);
			sb.append(NEW_LINE);
		}

		sb.append(NEW_LINE);
		sb.append("Version=2");
		sb.append(NEW_LINE);
		
		return sb.toString();
	}
}